﻿using System.Web.Mvc;
using MiddleMvcTask.Data;
using MiddleMvcTask.Data.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;

namespace MiddleMvcTask.AppStart
{
    /// <summary>
    /// Static class which load data from xml to application cashe
    /// </summary>
    public static class Casher
    {
        public static IRepository<User> Data { get; set; }
        
        /// <summary>
        /// Load users to application cashe
        /// </summary>
        public static void LoadStaticCache(IRepository<User> data)
        {
            Data = data ;

            // get data
            var users = LoadUsers();

            HttpRuntime.Cache.Insert(
                /* key */                "users",
                /* value */              users,
                /* dependencies */       null,
                /* absoluteExpiration */ Cache.NoAbsoluteExpiration,
                /* slidingExpiration */  Cache.NoSlidingExpiration,
                /* priority */           CacheItemPriority.NotRemovable,
                /* onRemoveCallback */   null);
        }
       
        private static ICollection<User> LoadUsers()
        {
            var users = Data.All();
            return users;
        }
    }
}